//
//  AssetDetailMainView.m
//  AppleMarket
//
//  Created by yangy on 01/11/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "AssetDetailMainView.h"
// view
#import "AssetMenuView.h"
#import "CoinAssetCell.h"
#import "CoinAssetCellSectionHeadView.h"
// videModel
#import "AssetDetailViewModel.h"
// model
#import "CoinAssetModel.h"

@interface AssetDetailMainView()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) AssetMenuView *menuView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) AssetDetailViewModel *viewModel;
@property (nonatomic,  strong) NSMutableArray<CoinAssetModel*> *coinModels;
@end

@implementation AssetDetailMainView
- (instancetype)initWithFrame:(CGRect)frame viewModel:(id) viewModel
{
    self = [super initWithFrame:frame];
    if(self){
        _viewModel = viewModel;
        _coinModels = [NSMutableArray arrayWithArray:_viewModel.coinModels];
        [self addSubview:self.tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
    }
    return self;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.coinModels.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CoinAssetCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CoinAssetCell class])];
    CoinAssetModel *coinModel = self.coinModels[indexPath.row];
    [cell updateWithModel:coinModel];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CoinAssetCellSectionHeadView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([CoinAssetCellSectionHeadView class])];
    view.buttonClickHandler = ^(BOOL isHidden) {
        // 隐藏币种0资产
        if(isHidden){
            // 隐藏账户资产
            [self.viewModel.coinModels enumerateObjectsUsingBlock:^(CoinAssetModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if(obj.Available == 0 && obj.Frozen == 0){ // 冻结资产和可用资产都为0
                    // 可用资产为0
                    [self.coinModels removeObject:obj];
                }
            }];
        }else{
            // 隐藏账户资产
            self.coinModels = [NSMutableArray arrayWithArray:self.viewModel.coinModels];
        }
        [self.tableView reloadData];
    };
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 53;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 101;
}

#pragma mark - lazy load
- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        [_tableView registerClass:[CoinAssetCell class] forCellReuseIdentifier:NSStringFromClass([CoinAssetCell class])];
        [_tableView registerClass:[CoinAssetCellSectionHeadView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([CoinAssetCellSectionHeadView class])];
        [_tableView setTableFooterView:[UIView new]];
        [_tableView setSeparatorColor:kColorWithHex(0xEFEFEF)];
        [_tableView setDataSource:self];
        [_tableView setDelegate:self];
        [_tableView setTableHeaderView:self.menuView];
    }
    return _tableView;
}

- (AssetMenuView *)menuView
{
    if(!_menuView){
        NSArray *items = @[
                           [AssetMenuItem menuItemTitle:@"冲币" image:[UIImage imageNamed:@"icon_push"] action:^{
                               [self.viewModel.pushClickSubject sendNext:nil];
                           }],
                           
                           [AssetMenuItem menuItemTitle:@"提币" image:[UIImage imageNamed:@"icon_pull"] action:^{
                               [self.viewModel.pullClickSubject sendNext:nil];
                           }],
                           
                           [AssetMenuItem menuItemTitle:@"钱包地址" image:[UIImage imageNamed:@"icon_wallet"] action:^{
                               [self.viewModel.walletClickSubject sendNext:nil];
                           }],
                           
                           [AssetMenuItem menuItemTitle:@"交易记录" image:[UIImage imageNamed:@"icon_record"] action:^{
                               [self.viewModel.recordClickSubject sendNext:nil];
                           }]
                           ];
        _menuView = [AssetMenuView menuViewItems:items frame:CGRectMake(0, 0, self.width, 118)];
    }
    return _menuView;
}

@end
